import { Component } from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {UserInfoService} from "../../../services/userInfo.service";
import {AppGlobal, AppService} from "../../../services/app.service";
import {UploadService} from "../../../services/upload.service";
import {User} from "../../../models/user-model";
import {ResDataModel} from "../../../models/res-data-model";
//import {ImagePicker} from "@ionic-native/image-picker";

/**
 * Generated class for the UserInfoPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-user-info',
  templateUrl: 'user-info.html',
})
export class UserInfoPage {

  userInfo:User;
  //profilePic:string;

  posts = [
    {
      postImageUrl: './assets/imgs/background/background-5.jpg',
      text: `我刚处理了一条XX任务，太开心了，快来给我点赞（今天不加班，准时溜了）`,
      date: 'November 5, 2016',
      likes: 12,
      comments: 4,
      timestamp: '11h ago'
    },
    {
      postImageUrl: './assets/imgs/background/background-3.jpg',
      text: '因为这个地方要输入文字，那我只能象征性的随便写写',
      date: 'October 23, 2016',
      likes: 30,
      comments: 64,
      timestamp: '30d ago'
    },
    {
      postImageUrl: './assets/imgs/background/background-4.jpg',
      date: 'June 28, 2016',
      likes: 46,
      text: `不知道该说点什么，祝大家新年快乐吧`,
      comments: 66,
      timestamp: '4mo ago'
    },
  ];

  constructor(public navCtrl: NavController,
              public viewCtrl: ViewController,
              public navParams: NavParams,
              private userInfoService: UserInfoService,
              private appService: AppService,
              private uploadService: UploadService
              //private imagePicker:ImagePicker
  ) {
    console.log('触发构造函数');
    this.userInfo = this.userInfoService.user;
    //this.profilePic = AppGlobal.myProfilePicUri;
  }
//沟崽子们
  ionViewDidLoad(){
    console.log('触发ionViewDidLoad');
  }

  ionViewWillEnter(){
    console.log('触发ionViewWillEnter');
  }

  ionViewDidEnter(){
    console.log('触发ionViewDidEnter');
  }

  ionViewWillLeave(){
    console.log('触发ionViewWillLeave');
  }

  ionViewDidLeave(){
    console.log('触发ionViewDidLeave');
  }

  ionViewWillUnload(){
    console.log('触发ionViewWillUnload');
  }

  //调试上传
  uploadProfilePic() {

    this.uploadService.startUploadImg((data)=>{

      //alert("上传成功：" + JSON.stringify(data));
      //修改头像的显示(这里显示的是复制前的图片)
      this.userInfo.profilePicUri = AppGlobal.platform=="iOS"?
          data.localPath.replace(/^file:\/\//, ''):data.localPath;//data.localPath

      //头像保存
      let postData = {
        fileName: data.fileName,
        fileSize: data.fileSize,
        fileSuffix: data.fileSuffix,
        downLoadPath: data.downLoadPath,
        filePath: data.filePath,
        fileId: data.fileId
      }
      this.appService.httpPost("AppUserCtrl_saveMyPhoto",postData,null,
        (res:ResDataModel)=>{
          let flag = res.datas[0].value;
          if(flag == "success") {
            this.appService.toast("头像保存成功");
            //alert("保存头像成功！");
          } else {
            console.error("保存头像失败");
            //alert("保存头像失败！");
          }
      },false)
    },(err)=>{
      alert("上传失败：" + JSON.stringify(err));
    },true,AppGlobal.myProfilePicName)
  }

  onClickProfilePic(){

  }

  dismiss() {
    this.viewCtrl.dismiss();
  }

  edit(){
    this.navCtrl.push("UserInfoEditPage");
  }

}
